package com.projectxxxxxxxxxx.service;


import java.util.Map.Entry;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.Binder;
import android.os.IBinder;
import android.os.SystemClock;
import android.util.Log;
import android.widget.Toast;

import com.projectxxxxxxxxxx.RoboApplication;
import com.projectxxxxxxxxxx.info.Info;

public class GetInfoService extends Service {

    public static final int INTERVAL = 10000; // 10 sec
    public static final int FIRST_RUN = 5000; // 5 seconds
    int REQUEST_CODE = 11223344;

    AlarmManager alarmManager;

    @Override
    public void onCreate() {
        super.onCreate();
        Log.v(this.getClass().getName(), "onCreate(..)");
        startAlarmManager();
    }

    @Override
    public void onStart(Intent intent, int startId) {
        super.onStart(intent, startId);
        Log.v(this.getClass().getName(), "onStart");
        Thread myThread = new Thread(new Runnable() {
            @Override
            public void run() {
                update();
            }
        });
		myThread.start();
    }

	private final IBinder mBinder = new LocalBinder();

	public class LocalBinder extends Binder {
		public GetInfoService getService() {
			return GetInfoService.this;
		}
	}

    @Override
    public IBinder onBind(Intent intent) {
        Log.v(this.getClass().getName(), "onBind(..)");
		return mBinder;
    }

    @Override
    public void onDestroy() {

        if (alarmManager != null) {
            Intent intent = new Intent(this, GetInfoService.class);
            alarmManager.cancel(PendingIntent.getBroadcast(this, REQUEST_CODE,
                    intent, 0));
        }
        Toast.makeText(this, "Service Stopped!", Toast.LENGTH_LONG).show();

        Log.v(this.getClass().getName(), "Service onDestroy(). Stop AlarmManager at " + new java.sql.Timestamp(System.currentTimeMillis()).toString());
    }

    private void startAlarmManager() {
        Intent intent = new Intent(this, GetInfoService.class);
        PendingIntent pendingIntent = PendingIntent.getService(RoboApplication.getInstance(), REQUEST_CODE, intent, 0);
        alarmManager = (AlarmManager) getSystemService(ALARM_SERVICE);
        alarmManager.setRepeating(
                AlarmManager.ELAPSED_REALTIME_WAKEUP,
                SystemClock.elapsedRealtime() + FIRST_RUN,
                INTERVAL, pendingIntent);
        Toast.makeText(this, "Service Started.", Toast.LENGTH_LONG).show();
        Log.v(this.getClass().getName(), "AlarmManger started at " + new java.sql.Timestamp(System.currentTimeMillis()).toString());
    }

	public String get(Info info, String name) {

		return info.get().get(name);

		// switch (info) {
		// case CPU_USAGE:
		// return Info.CPU_USAGE.get().get(name);
		// case CPU_DATA:
		// return Info.CPU_DATA.get().get(name);
		// case MEMORY_USAGE:
		// return Info.MEMORY_USAGE.get().get(name);
		// case MEMORY_DATA:
		//
		// case STORAGE_INTERNAL_USAGE:
		// case STORAGE_INTERNAL_DATA:
		// case STORAGE_EXTERNAL_USAGE:
		// case STORAGE_EXTERNAL_DATA:
		// case BATTERY:
		// case SYSTEM:
		//
		// default:
		// return "";
		// }
	}

    private void update() {
        Log.i(getClass().toString(), "INFO: ");
        for (Info info : Info.values()) {
            
			switch (info) {
			case CPU_USAGE:
			case CPU_DATA:
			case MEMORY_USAGE:
			case MEMORY_DATA:
			case STORAGE_INTERNAL_USAGE:
			case STORAGE_INTERNAL_DATA:
			case STORAGE_EXTERNAL_USAGE:
			case STORAGE_EXTERNAL_DATA:
			case BATTERY:
			case SYSTEM:
			case NETWORK:

				for (Entry<String, String> entry : info.get().entrySet()) {
					String value = entry.getValue();
					String key = entry.getKey();

					Log.i(getClass().toString(), key + "  : " + value);
				}
				break;

                default:
                    break;
            }
        }
           
    }

    // final Handler myHandler = new Handler(); // автоматически привязывается к
    // текущему потоку.
    // final TextView txtResult = (TextView)findViewById(R.id.txtResult);
    // Thread myThread = new Thread(new Runnable() {
    // final String result = doLongAndComplicatedTask();
    // myHandler.post(new Runnable() { // используя Handler, привязанный к
    // UI-Thread
    // @Override
    // public void run() {
    // txtResult.setText(result); // выполним установку значения
    // }
    // });
    // });
    //
    // myThread.start();

}
